namespace SinaFinanceBaseClassLibrary
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Runtime.CompilerServices;
    using System.Xml.Linq;

    public class LocalFileStorage
    {
        public static string filePath = string.Empty;
        private const string MarketDetailInfoFileName = "Market_DetailInfo.txt";
        private const string MarketFileName = "Market_BasicInfo.txt";
        private const string SHANGZHENG = "000001";
        private const string SHENZHI = "399001";

        public static void CreateFileInDirectory(string DirectoryPath, string FileName)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (string.IsNullOrEmpty(DirectoryPath))
                {
                    IsolatedStorageFileStream stream = file.CreateFile(FileName);
                    file.Dispose();
                }
                else
                {
                    file.CreateFile(Path.Combine(DirectoryPath, FileName)).Close();
                    file.Dispose();
                }
            }
        }

        public static void CreateMarketDetailInfoFile(string resultXml)
        {
            WriteFile(Path.Combine(string.Empty, "Market_DetailInfo.txt"), resultXml);
        }

        public static void CreateMarketInfoFiles()
        {
            CreateStorageDirectory("000001");
            CreateStorageDirectory("399001");
        }

        public static void CreateStockKLineInfoFile(string resultXml)
        {
        }

        public static void CreateStockMarketBasicInfoFile(string resultXml)
        {
            WriteFile(Path.Combine(string.Empty, "Market_BasicInfo.txt"), resultXml);
        }

        public void CreateStockTimeSharingInfoFiles(string resultXml)
        {
            IEnumerator enumerator = (IEnumerator) XElement.Parse(resultXml).Nodes().GetEnumerator();
            while (enumerator.MoveNext())
            {
                XNode node = (XNode) enumerator.Current;
                XElement element2 = (XElement) node;
                string directoryPath = element2.Name.ToString();
                string fileName = "TimesharingRecord.txt";
                CreateStorageDirectory(directoryPath);
                CreateFileInDirectory(directoryPath, fileName);
                filePath = Path.Combine(directoryPath, fileName);
                WriteFile(filePath, resultXml);
            }
        }

        public static void CreateStorageDirectory(string DirectoryPath)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!file.DirectoryExists(DirectoryPath))
                {
                    file.CreateDirectory(DirectoryPath);
                }
            }
        }

        public static List<DB_RealTime> ReadMarketDetailInfoFile()
        {
            List<DB_RealTime> marketBaseData = null;
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!file.FileExists("Market_DetailInfo.txt"))
                {
                    return marketBaseData;
                }
                using (StreamReader reader = new StreamReader((Stream) file.OpenFile("Market_DetailInfo.txt", (FileMode) FileMode.Open, (FileAccess) FileAccess.Read)))
                {
                    marketBaseData = DB_SinaServiceHelp.GetMarketBaseData(reader.ReadToEnd());
                }
            }
            return marketBaseData;
        }

        public static List<DB_BaseInfo> ReadMarketInfoFile()
        {
            List<DB_BaseInfo> topInfoData = null;
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!file.FileExists("Market_BasicInfo.txt"))
                {
                    return topInfoData;
                }
                using (StreamReader reader = new StreamReader((Stream) file.OpenFile("Market_BasicInfo.txt", (FileMode) FileMode.Open, (FileAccess) FileAccess.Read)))
                {
                    topInfoData = DB_SinaServiceHelp.GetTopInfoData(reader.ReadToEnd());
                }
            }
            return topInfoData;
        }

        private static void WriteFile(string filePath, string content)
        {
            IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            try
            {
                if (userStoreForApplication.FileExists(filePath))
                {
                    userStoreForApplication.DeleteFile(filePath);
                }
                if (!userStoreForApplication.FileExists(filePath))
                {
                    IsolatedStorageFileStream stream = userStoreForApplication.CreateFile(filePath);
                    stream.Close();
                    stream.Dispose();
                }
                using (StreamWriter writer = new StreamWriter((Stream) userStoreForApplication.OpenFile(filePath, (FileMode) FileMode.Open, (FileAccess) FileAccess.Write)))
                {
                    writer.WriteLine(content);
                    writer.Close();
                    writer.Dispose();
                    userStoreForApplication.Dispose();
                }
            }
            catch
            {
            }
            finally
            {
                if (userStoreForApplication != null)
                {
                    userStoreForApplication.Dispose();
                }
            }
        }

        public delegate void WriteFileHandler();
    }
}
